package com.mycase.im.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mycase.im.boot.entity.database.ImGroupMessageBody;
import com.mycase.im.boot.mapper.GroupMessageBodyMapper;
import com.mycase.im.boot.mapper.UserMapper;
import com.mycase.im.boot.service.GroupMessageBodyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author ZhuHJay
 * @Date 2021/9/4 15:11
 */
@Service
public class GroupMessageBodyServiceImpl extends ServiceImpl<GroupMessageBodyMapper, ImGroupMessageBody> implements GroupMessageBodyService {

    @Override
    public ImGroupMessageBody getOne(String userId, String messageBodyId) {
        QueryWrapper<ImGroupMessageBody> imGroupMessageBodyQueryWrapper = new QueryWrapper<>();
        imGroupMessageBodyQueryWrapper
                .eq("user_id", userId)
                .eq("message_body_id", messageBodyId);
        return getOne(imGroupMessageBodyQueryWrapper);
    }

    @Override
    public void unReadMessageConvertById(List<Integer> unReadsId) {
        // 如果是空的, 不管他, 可能是多请求的, 直接放行
        if( unReadsId == null ){
            return;
        }
        UpdateWrapper<ImGroupMessageBody> imGroupMessageBodyUpdateWrapper = new UpdateWrapper<>();
        unReadsId.forEach(unReadId -> {
            imGroupMessageBodyUpdateWrapper
                    .set("read_status", 1)
                    .eq("id", unReadId);
            update(imGroupMessageBodyUpdateWrapper);
            // 清除
            imGroupMessageBodyUpdateWrapper.clear();
        });
    }

}
